<?php
namespace Modules\Admin\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;

class OrderController extends Controller{
    //订单列表
    public function orderListApi(Request $request){
        try{
            $page = $request->input("page");
            $size = $request->input("limit");
            $orderSn = $request->input("order_sn",false);
            $username = $request->input("username",false);
            $phone = $request->input("phone",false);
            $address = $request->input("address",false);
            $orderStatus = $request->input("order_status",'all');
            $payment = $request->input("payment",'all');
            $startDate = $request->input("start_date",false);
            $endDate = $request->input("end_date",false);

            $query = Order::query();
            if ($orderSn){
                $query->where("order_sn",$orderSn);
            }
            if ($username){
                $query->where("username",$username);
            }
            if ($phone){
                $query->where("phone",$phone);
            }
            if ($address){
                $query->where("address",'like',"%{$address}%");
            }
            if ($orderStatus!='all'){
                $query->where("order_status",$orderStatus);
            }
            if ($payment!='all'){
                $query->where('payment',$payment);
            }
            if ($startDate){
                $query->whereDate("order_time",">=",$startDate);
            }
            if ($endDate){
                $query->whereDate("order_time","<=",$endDate);
            }
            $orderCount = $query->count();
            $orderData = $query->skip(($page-1)*$size)->take($size)->orderBy('id','desc')->get();
            return self::jsondata($orderCount,$orderData);
        }catch (\Exception $e){
            Log::error(500);
            return self::error(500);
        }
    }

    //订单导出
    public function orderExport(Request $request){
        try{
            $orderSn = $request->input("order_sn",false);
            $username = $request->input("username",false);
            $phone = $request->input("phone",false);
            $address = $request->input("address",false);
            $orderStatus = $request->input("order_status",'all');
            $payment = $request->input("payment",'all');
            $startDate = $request->input("start_date",false);
            $endDate = $request->input("end_date",false);
            $query = Order::query();
            if ($orderSn){
                $query->where("order_sn",$orderSn);
            }
            if ($username){
                $query->where("username",$username);
            }
            if ($phone){
                $query->where("phone",$phone);
            }
            if ($address){
                $query->where("address",'like',"%{$address}%");
            }
            if ($orderStatus!='all'){
                $query->where("order_status",$orderStatus);
            }
            if ($payment!='all'){
                $query->where('payment',$payment);
            }
            if ($startDate){
                $query->whereDate("order_time",">=",$startDate);
            }
            if ($endDate){
                $query->whereDate("order_time","<=",$endDate);
            }
            $orderData = $query->with('orderGoods')->with('user')->get();
            $cellData = [
                ['序号', '订单号', '订单总价','支付方式','商品信息','商品总件', '购买用户', '收货人', '联系电话', '收货地址','物流公司','物流单号','订单状态','下单时间','支付时间','发货时间','收货时间']
            ];
            $i = 1;
            foreach ($orderData as $key=>$item){
                if ($item['payment']=='wechat'){
                    $payType = "微信支付";
                }else{
                    $payType = "积分支付";
                }
                $goodsInfo = '';
                foreach ($item['orderGoods'] as $goods){
                    $goodsInfo = $goodsInfo."[{$goods['goods_name']},{$goods['sku_name']},{$goods['color_name']},{$goods['goods_num']}]";
                }
                if ($item['order_status']==-1){
                    $orderTxt = '已取消';
                }elseif ($item['order_status']==0){
                    $orderTxt = '待支付';
                }elseif ($item['order_status']==1){
                    $orderTxt = '待发货';
                }elseif ($item['order_status']==2){
                    $orderTxt = '待签收';
                }elseif ($item['order_status']==3){
                    $orderTxt = '已签收';
                }elseif ($item['order_status']==4){
                    $orderTxt = '已评价';
                }elseif ($item['order_status']==7){
                    $orderTxt = '已退款';
                }
                $array = [$i,"\t".$item['order_sn']."\t",'¥'.$item['order_amount'],$payType,"\t".$goodsInfo."\t",$item['goods_num'].'件',$item['user']['nickname'],$item['username'],"\t".$item['phone']."\t","\t".$item['address']."\t",$item['express_company'],$item['express_code'],$orderTxt,$item['order_time'],$item['pay_time'],$item['send_time'],$item['finish_time']];
                array_push($cellData,$array);
                $i++;
            }
            Excel::create(iconv('UTF-8', 'GBK', '订单数据表'), function ($excel) use ($cellData) {
                $excel->sheet('score', function ($sheet) use ($cellData) {
                    $sheet->rows($cellData);
                });
            })->store('xls')->export('xls');
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //取消订单
    public function orderCloseApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $order = Order::find($id);
            if (!$order || $order->order_status!=0)return self::error(1027);
            $order->order_status=-1;
            $order->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //订单发货
    public function orderSendApi(Request $request){
        try{
            $id = $request->input("id",false);
            $expressNo = $request->input("express_no",false);
            $expressCompany = $request->input("express_company",false);
            if (!$id || !$expressCompany || !$expressNo)return self::error(1000);
            $order = Order::find($id);
            if (!$order || $order->order_status!=1)return self::error(1027);
            $order->order_status = 2;
            $order->express_company = $expressCompany;
            $order->express_code= $expressNo;
            $order->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //查询订单数据
    public function orderTongJi(){
        try{
            $orderClose = Order::where("order_status",-1)->count();
            $orderDF = Order::where("order_status",0)->count();
            $orderYF = Order::where("order_status",1)->count();
            $orderDQ = Order::where("order_status",2)->count();
            $orderQS = Order::where("order_status",3)->count();
            $orderPJ = Order::where("order_status",4)->count();
            $orderTK = Order::where("order_status",7)->count();
            return self::success(["qx"=>$orderClose,'wf'=>$orderDF,'yf'=>$orderYF,'dq'=>$orderDQ,'qs'=>$orderQS,'pj'=>$orderPJ,'tk'=>$orderTK]);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }
}